package cn.com.weather.nf.util;

import org.springframework.util.StringUtils;

import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class NameUtil {

    /**
     *
     * Convert the given name to lower case.
     * By default, conversions will happen within the US locale.
     * @param name the original name
     * @return the converted name
     * @since 4.2
     */
    protected static String lowerCaseName(String name) {
        return name.toLowerCase(Locale.US);
    }

    /**
     * Convert a name in camelCase to an underscored name in lower case.
     * Any upper case letters are converted to lower case with a preceding underscore.
     * @param name the original name
     * @return the converted name
     * @since 4.2
     * @see #lowerCaseName
     */
    public static String underscoreName(String name) {
        if (!StringUtils.hasLength(name)) {
            return "";
        }
        StringBuilder result = new StringBuilder();
        result.append(lowerCaseName(name.substring(0, 1)));
        for (int i = 1; i < name.length(); i++) {
            String s = name.substring(i, i + 1);
            String slc = lowerCaseName(s);
            if (!s.equals(slc)) {
                result.append("_").append(slc);
            }
            else {
                result.append(s);
            }
        }
        return result.toString();
    }

    private static Pattern underAndChar = Pattern.compile("_");


    public static String humpName(String name) {

        if (!StringUtils.hasLength(name)) {
            return "";
        }

        StringBuilder buff = new StringBuilder(name.length());
        Stream.of( underAndChar.split(name) )
                .forEach(input -> {
                    char c = input.charAt(0);
                    buff.append(Character.toUpperCase(c));
                    if (input.length() > 1) {
                        buff.append(input.substring(1));
                    }
                });

        return buff.toString();

    }

}
